<template>
	<view>
		<!-- 我的 -->
		<view class="top">
			<!-- 头部 -->
			<view class="topInner1">
				<image class="topImg" :src="user.imgSrc" @click="chooseImage"></image>
			</view>
			<view class="topInner2">
				<view style="margin-bottom: 7rpx;">{{user.nickName}}</view>
				<uni-icons type="phone-filled" size="20"></uni-icons>
				<text>{{user.tel}}</text>
			</view>
			<view class="exit" @click.native='exit'>
				退出登录
			</view>
		</view>
		<view class="orderPart">
			<!-- 我的订单 -->
			<view>
				<view class="orderItem" @click="jumpMyOrder">我的订单</view>
				<view class="orderItem" @click="jumpUserAddress">我的地址</view>
				<view class="orderItem" @click="jumpMyBalance">我的余额</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad() {
			// this.getUserInfo();
			this.selUserInfo();
		},
		data() {
			return {
				user: {}
			}
		},
		methods: {
			jumpMyBalance() {
				uni.navigateTo({
					url: "/pages/myBalance/myBalance"
				})
			},
			jumpUserAddress() {
				uni.navigateTo({
					url: "/pages/userAddress/userAddress"
				})
			},
			jumpMyOrder() {
				uni.navigateTo({
					url: "/pages/myOrder/myOrder"
				})
			},
			chooseImage() {
				uni.chooseImage({
					count: 1, // 默认9
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (res) => {
						this.tempFilePaths = res.tempFilePaths;
						// 开始上传图片
						this.uploadImage(this.tempFilePaths[0]);
					}
				});
			},
			uploadImage(fileUrl) {
				uni.uploadFile({
					url: 'api/appuser/uploadImage', // 后端上传接口地址
					filePath: fileUrl,
					name: 'file',
					formData: {}, // 可以附带其他表单数据
					success: (res) => {
						// console.log('图片上传成功', res);
						// console.log(res.data);
						let jwt = uni.getStorageSync('authority');
						uni.request({
							url: "api/appuser/updateUserPic",
							data: res.data,
							method: 'POST',
							header: {
								"authority": jwt
							},
							success: (res) => {
								if (res.data.code == 200) {
									uni.showToast({
										title: "头像修改成功",
										icon: 'success'
									})
									this.selUserInfo();
								}
							}
						})
					},
					fail: (err) => {
						console.log('图片上传失败', err);
					}
				});
			},
			exit() {
				let jwt = uni.getStorageSync('authority');
				uni.request({
					url: "api/appuser/setUserLastLogin",
					header: {
						"authority": jwt
					},
					success: (res) => {
						// console.log(res);
					}
				})
				uni.reLaunch({
					url: "/pages/login/login"
				})
			},
			getUserInfo() {
				this.user = uni.getStorageSync('userInfo');
				// console.log(this.user);
			},
			selUserInfo() {
				let jwt = uni.getStorageSync('authority');
				uni.request({
					url: "api/appuser/selUserInfo",
					header: {
						"authority": jwt
					},
					method: 'GET',
					success: (res) => {
						// console.log(res.data.data);
						this.user = res.data.data;
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #f4f4f6;
	}

	.top {
		width: 700rpx;
		height: 230rpx;
		background-color: #fff;
		border-radius: 90rpx;
		margin: 0 auto;
		margin-top: 50rpx;

		.topInner1 {
			float: left;
			margin-left: 29rpx;
			margin-top: 30rpx;

			.topImg {
				border-radius: 1200rpx;
				width: 150rpx;
				height: 150rpx;
			}
		}

		.topInner2 {
			float: left;
			margin-top: 74rpx;
			margin-left: 30rpx;
		}

		.exit {
			float: right;
			margin-top: 94rpx;
			margin-right: 30rpx;
			color: #007bff;
		}
	}

	.orderPart {
		width: 750rpx;
		height: 100rpx;
		/* background-color: antiquewhite; */
		border-radius: 50rpx;
		margin-top: 30rpx;

		.orderItem {
			float: left;
			height: 100rpx;
			width: 170rpx;
			background-color: #fff;
			text-align: center;
			line-height: 100rpx;
			margin-right: 18rpx;
			border-radius: 50rpx;
		}

	}
</style>